import { readyGameWS } from "./index"
import {set,get ,del,client} from "../redis";
readyGameWS.on("connection", (client, req) => {
    // console.log("有客户端连接成功了", client)
    //对客户端连接对象进行message事件监听
    //msg: 由客户端发送给服务端的数据
    // client.id = _msg.id
    console.log("url",req.url);
    let id = ((req.url as any).split('?id=')[1]);
    console.log("id", id);
    
    (client as any)['id'] = id
    
    client.send(JSON.stringify('你好'))
    client.on("message",async _msg => {
        console.log(2222);
        
        let msg = JSON.parse(_msg.toString())
        console.log("msg", msg);
        // console.log("client", client);
        // console.log("req", req);
       
        // console.log("msg2", msg.url);
        if(msg.key == "join") {
            let roomId = msg['roomId'];
            console.log("roomId",roomId);
            
            let roomIdValueRedis = (await get(roomId)) as any;
            console.log("roomIdValueRedis",roomIdValueRedis);
            
            roomIdValueRedis = roomIdValueRedis
            let roleName = msg.role.name;
            // let roleRedis = 
            if(!roomIdValueRedis[roleName].player.id) {
                console.log("没有id",roomIdValueRedis);
                roomIdValueRedis[roleName].player.nickname = msg.player.nickname;
                roomIdValueRedis[roleName].player.avatar = msg.player.avatar;
                roomIdValueRedis[roleName].player.id = msg.player.id;
                set(roomId,JSON.stringify(roomIdValueRedis));
                readyGameWS.clients.forEach( (C)=> {
                    // if (C.readyState === WebSocket.OPEN) {
                      C.send(JSON.stringify({data:roomIdValueRedis,key:"joinSuccess",code:200,roomId}));
                    // }
                  });
                // client.send(JSON.stringify({data:roomIdValueRedis,key:"joinSuccess",code:200}))
            }else {
                client.send("已经有了")
            }
        } else if(msg.key == "queryReadyRoom") {
            console.log("查询了");
            
            let roomId = msg['roomId'];
            let roomIdValueRedis = (await get(roomId)) as any;
            // if(!)
            console.log("roomIdValueRedis",roomIdValueRedis);
            
            client.send(JSON.stringify({data:roomIdValueRedis,key:"readyRoomInfo",code:200}))
        } else if(msg.key == "changeClue") {
            let gameId = msg['gameId'];
            let roomIdValueRedis = (await get(gameId)) as any;
            console.log("changeClue",roomIdValueRedis);
            let clue = msg.allClue
            let _data = {...roomIdValueRedis.data}
            
            let players = msg.players
            _data.allClue = clue;
            console.log("_data",_data);
            console.log("readyGameWS",players);
            
            // readyGameWS.clients.forEach((c as any) => {
               
            //         for(let i=0;i<players.length;i++) {
            //             if(players[i] == c.id){

            //             }
            //         }
                
            // })

            readyGameWS.clients.forEach((c)=>{
                for(let i=0;i<players.length;i++) {
                        if(players[i] == (c as any).id){
                            console.log("等于", id);
                            c.send(JSON.stringify({
                                data:_data,
                                key:"newClue",
                                code:200,
                                gameId}))
                        }
                }
            })



            // set(gameId,_data)
            // let value = {
            //     scriptName,
            //     allClue: script.clue,
            //     role,
            //     dmId:id
            // }
            //value 所有剧本信息，所有线索信息，分配线索信息
            set(gameId,JSON.stringify({data:_data}))
        }
    })

})